﻿using System.Text;
using FR.CodeVisualizer.SBVisualizer;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System;
using FR.CodeVisualizer.Views;
using System.Windows.Forms;
using System.Xml.Linq;
using FR.CodeVisualizer.ObjectSource;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(StringBuilderVisualizer), typeof(VisualizerObjectSource), Target = typeof(StringBuilder), Description = "FR Visualizers - StringBuilder")]
[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(StringBuilderVisualizer), typeof(VisualizerObjectSource), Target = typeof(String), Description = "FR Visualizers - String")]
[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(XDocumentVisualizer), typeof(XDoccumentVisualizerObjectSource), Target = typeof(XDocument), Description = "FR Visualizers - XML")]

namespace FR.CodeVisualizer.SBVisualizer
{
    public class StringBuilderVisualizer : DialogDebuggerVisualizer
    {
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(StringBuilderVisualizer), typeof(VisualizerObjectSource), true);
            visualizerHost.ShowVisualizer();
        }

        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            if (windowService == null)
                throw new ArgumentNullException("windowService");
            if (objectProvider == null)
                throw new ArgumentNullException("objectProvider");

            object obj = objectProvider.GetObject();

            if (obj != null)
            {
                //inicializar o visualizador
                using (FormMainTextEdit main = new FormMainTextEdit())
                {
                    main.Init(obj.ToString(), text: obj is StringBuilder ? " - StringBuilder Visualizer" : " - String Visualizer", editable: objectProvider.IsObjectReplaceable, showSqlPanel: true);
                    
                    //mostrar o visualizer
                    DialogResult dr = windowService.ShowDialog(main);

                    if (objectProvider.IsObjectReplaceable && dr == DialogResult.OK)
                    {
                        if (obj is StringBuilder)
                        {
                            (obj as StringBuilder).Clear();
                            (obj as StringBuilder).Append(main.SourceCodeText);
                        }
                        else
                            obj = main.SourceCodeText;

                        objectProvider.ReplaceObject(obj);
                    }
                }
            }
        }
    }

    public class XDocumentVisualizer : DialogDebuggerVisualizer
    {
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(XDocumentVisualizer), typeof(XDoccumentVisualizerObjectSource));
            visualizerHost.ShowVisualizer();
        }

        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            if (windowService == null)
                throw new ArgumentNullException("windowService");
            if (objectProvider == null)
                throw new ArgumentNullException("objectProvider");

            object obj = objectProvider.GetObject();

            if (obj != null)
            {
                //inicializar o visualizador
                using (FormMainTextEdit main = new FormMainTextEdit())
                {
                    main.Init(obj.ToString(), text: " - Xml Visualizer", editable: objectProvider.IsObjectReplaceable);
                    main.SetLanguage(SupportedLanguage.XML);

                    //mostrar o visualizer
                    DialogResult dr = windowService.ShowDialog(main);

                    if (objectProvider.IsObjectReplaceable && dr == DialogResult.OK)
                    {
                        Stream outgoingData = new MemoryStream();
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(outgoingData, main.SourceCodeText);

                        objectProvider.TransferData(outgoingData);
                    }
                }
            }
        }
    }
}
